﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;
using EluZSProvider;
using System.Configuration;
namespace WebSys.UI.OfficeFlow
{
    public class AcceptbusinessSkin : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        DropDownList flowdownlist;
        Button acceptbtn;
        TextBox taskname, taskid;
        PlaceHolder showshouliHolder, wuquanHolder;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            LoginHelper.CheckLogin();
            InitControl();
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                Initcy();
            }
        }

        private void InitControl()
        {
            flowdownlist = (DropDownList)FindControl("flowdownlist");
            showshouliHolder = (PlaceHolder)FindControl("showshouliHolder");
            wuquanHolder = (PlaceHolder)FindControl("wuquanHolder");
            taskname = (TextBox)FindControl("taskname");
            taskid = (TextBox)FindControl("taskid");
            acceptbtn = (Button)FindControl("acceptbtn");
            acceptbtn.Click += acceptbtn_Click;
        }

        private void Initcy()
        {
            string ajlx = ConfigurationManager.AppSettings["ZSAnJianType"].ToString();
            DataTable dt = new DataTable();
            if (!_EluZSDataProvider.IsZSAdmin(UserId) && !_EluZSDataProvider.IsNetZSAdmin(UserId))
            {
                dt = _FlowFactoryProvider.NewGetStartWorkFlowListByUserId(UserId, ajlx);
            }
            else {
                dt = _FlowFactoryProvider.GetDT(@"select m.* from st_workflow m where m.rot=0 and m.wname in(" + ajlx + ") Order By wname Desc");
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                flowdownlist.DataSource = dt;
                flowdownlist.DataTextField = "WNAME";
                flowdownlist.DataValueField = "wid";
                flowdownlist.DataBind();
            }
            else
            {
                showshouliHolder.Visible = false;
                wuquanHolder.Visible = true;
            }
        }

        private void acceptbtn_Click1(object sender, EventArgs e)
        {
            string userid = UserId;
            string iid = "", wiid = "";
            string wid = flowdownlist.SelectedValue;
            string ownername = taskname.Text;
            string ownerid = taskid.Text;

            if (_FlowFactoryProvider.CreateInstance(wid, userid, ownername, ref iid, ref wiid))
            {
                string csql = "select count(*) from st_workflow where wname='房屋征收补偿决定' and rot=0 and wid='" + wid + "' ";
                if (Convert.ToInt32(_FlowFactoryProvider.GetSomeStr("(" + csql + ")", "*", "")) > 0)
                {
                    if (!string.IsNullOrEmpty(ownerid))
                    {
                        _FlowFactoryProvider.InsertIDGL(iid, ownerid);
                        _FlowFactoryProvider.Insertformdata(iid, ownerid);
                        _FlowFactoryProvider.InsertFWZSBUJDS(iid, ownerid);
                    }
                }
                base.Page.Response.Redirect("Transactbusiness.aspx");
            }

        }

        private void acceptbtn_Click(object sender, EventArgs e)
        {
            string userid = UserId;
            string iid = "", wiid = "";
            string wid = flowdownlist.SelectedValue;
            string ownername = taskname.Text;
            string ownerid = taskid.Text;
            string pid = _FlowFactoryProvider.GetOneStrString("zs_prono", "pid", "id", ownerid);

            string wname = _FlowFactoryProvider.GetSomeStr("st_workflow", "wname", " and wid='" + wid + "' and rot=0");


            if (_FlowFactoryProvider.CreateInstance(wid, userid, ownername, ref iid, ref wiid))
            {
                if (!string.IsNullOrEmpty(ownerid))
                {
                    _FlowFactoryProvider.InsertIDGL(iid, ownerid);
                    if (wname == "房屋征收程序申请" || wname == "房屋征收立项申请")
                    {
                        _FlowFactoryProvider.LiXiang(iid, ownerid);
                        _FlowFactoryProvider.InsterZSFile(userid, iid, ownerid, wname,1);
                    }
                    if (wname == "房屋征收补偿决定审批(区县预审)")
                    {
                        //zs_zsperson  id
                        _FlowFactoryProvider.ZSBuChang(iid, ownerid);
                       
                    }
                    if (wname == "房屋征收补偿决定审批(预审)")
                    {
                        //zs_zsperson  id
                        _FlowFactoryProvider.ZSBuChang(iid, ownerid);
                       
                    }
                    if (wname == "房屋征收补偿实施方案申请")
                    {
                        _FlowFactoryProvider.ShiShi(iid, ownerid);
                        _FlowFactoryProvider.InsterZSFile(userid, iid, ownerid, wname,7);
                    }
                    if (wname == "房屋征收补偿资金监管")
                    {
                        //ZS_BCBASICINFO id=ownerid
                        _FlowFactoryProvider.ZiJin(iid, ownerid);
                        
                    }
                    if (wname == "房屋征收冻结申请")
                    {
                        _FlowFactoryProvider.DongJie(iid, ownerid);
                        _FlowFactoryProvider.InsterZSFile(userid, iid, ownerid, wname,1);
                    }
                    if (wname == "房屋征收决定申请(区县)")
                    {
                        //zs_prono id
                       _FlowFactoryProvider.ZSJueDing(iid, ownerid);
                       _FlowFactoryProvider.InsterZSFile(userid, iid, ownerid, wname,9);
                    }
                    if (wname == "房屋征收决定申请")
                    {
                        //zs_prono id=ownerid
                        _FlowFactoryProvider.ZSJueDing(iid, ownerid);
                        _FlowFactoryProvider.InsterZSFile(userid, iid, ownerid, wname,9);
                    }
                    if (wname == "房屋征收补偿决定")
                    {
                        _FlowFactoryProvider.Insertformdata(iid, ownerid);
                        _FlowFactoryProvider.InsertFWZSBUJDS(iid, ownerid);                        
                    } if (wname=="房屋征收补偿方案申请")
                    {
                        _FlowFactoryProvider.ZSBCFangAn(iid, ownerid);
                        _FlowFactoryProvider.InsterZSFile(userid, iid, ownerid, wname,6);
                    }
                }
                base.Page.Response.Redirect("Transactbusiness.aspx");
            }

        }

        #region 立项
        public void LiXiang(string iid, string ownerid)
        {
            string sql = @"insert into UT_房屋征收立项审批表(IID,input_index,申请单位,项目名称,项目地点,征收部门,征收实施单位,项目批准文号,规划许可证编号,用地批准文号)
select '" + iid + @"' IID,'0', 申请单位,项目名称,项目地址,征收部门,征收实施单位,项目批文号,用地规划许可证号,国有土地使用权批准文件 from ZS_PROJECT
where id='" + ownerid + "'";
            _FlowFactoryProvider.RunSqlNon(sql);
        }
        #endregion

        #region 冻结
        public void DongJie(string iid, string ownerid)
        {
            //房屋征收冻结通知书
            string sql = @"insert into UT_房屋征收立项审批表(IID,input_index,项目名称,征收部门)select '" + iid + "' IID,'0',项目名称,征收部门 from ZS_PROJECT where id='" + ownerid + "'";
            _FlowFactoryProvider.RunSqlNon(sql);
            // sql = "insert into UT_房屋征收准备申请(IID,申请冻结期限,申请冻结期限2,征收冻结范围) select '"+iid+"' IID,冻结期限开始,冻结期限结束,冻结征收范围 from ZS_PROJECT where id='"+ownerid+"'";

            //房屋征收冻结范围申请
            sql = @"insert into UT_房屋征收准备申请(IID,input_index,申请单位,联系电话,申请冻结期限,申请冻结期限2,征收冻结范围,权证户数,户口户数,合计,住宅,非住宅)
select '" + iid + @"'IID,'0',申请单位,电话,冻结期限开始,冻结期限结束,冻结征收范围,被征收居民权证户数,被征收居民户口户数,被征收房屋面积合计,被征收房屋面积住宅,被征收房屋面积非住宅
from ZS_PROJECT where id='" + ownerid + "'";
            _FlowFactoryProvider.RunSqlNon(sql);
        }
        #endregion

        #region 实施
        public void ShiShi(string iid, string ownerid)
        {
            //--征收补偿实施方案审批表
            string sql = @"insert into ut_房屋征收补偿实施方案审批表(IID,input_index,项目名称,项目地点,征收部门,征收实施单位,征收范围,合计,住宅,非住宅,
权证,工企单位,征收补偿资金概算,开户银行,到帐金额,产权置换房套数,保障房源地点,保障房源套数)
select '" + iid + @"' IID,'0',
(select 项目名称 from zs_project where id='" + ownerid + @"')项目名称,
(select 项目地址 from zs_project where id='" + ownerid + @"')项目地点,
(select 征收部门 from zs_project where id='" + ownerid + @"')征收部门,
(select 征收实施单位 from zs_project where id='" + ownerid + @"')征收实施单位,
征收范围,
房屋建筑面积,
房屋建筑面积住宅,
房屋建筑面积非住宅,
权证户数,
工企单位,
征收补偿资金,
(select 开户银行 from ZS_BCBASICINFO where pnid='" + ownerid + @"')开户银行,
(select 到账金额 from zs_bcbasicinfo where pnid='" + ownerid + @"')到账金额,
产权调换房源数量,
保障房源地点,
保障房源数量
from ZS_BCFAQS where pid='" + ownerid + @"' and  补偿实施=1";
            _FlowFactoryProvider.RunSqlNon(sql);

            //--征收补偿实施方案审核意见书
            sql = @"insert into UT_房屋征收立项审批表(IID,input_index,申请单位,项目名称)
                    select '" + iid + "' IID,'0',申请单位,项目名称 from zs_project where id='" + ownerid + "'";
            _FlowFactoryProvider.RunSqlNon(sql);


        }
        #endregion

        #region 资金
        public void ZiJin(string iid, string ownerid)
        {
            string pid = _FlowFactoryProvider.GetOneStrString("ZS_BCBASICINFO", "pnid", "id", ownerid);
            string sql = @"insert into ut_征收项目补偿资金监管联系单(IID,input_index,申请单位,项目地点,开户单位,项目联系人,总建筑面积,项目编号,总补偿款,备注)
select '" + iid + @"' IID,'0',
(select 申请单位 from zs_project where id='" + pid + @"')申请单位,
(select 项目地址 from zs_project where id='" + pid + @"')项目地点,
开户银行,征收实施单位,
(select 被征收房屋面积合计 from zs_project where id='" + pid + @"')被征收房屋面积合计,
(select 立项号 from zs_project where id='" + pid + @"')项目编号,
评估总补偿款,备注
from ZS_BCBASICINFO where id='" + ownerid + "'";
            _FlowFactoryProvider.RunSqlNon(sql);
        }
        #endregion

        #region 征收决定
        public void ZSJueDing(string iid, string ownerid)
        {
            string pid = _FlowFactoryProvider.GetOneStrString("zs_prono", "pid", "id", ownerid);
            string sql = @"insert into ut_房屋征收决定审批表(IID,input_index,申请单位,项目名称,项目地点,征收部门,征收实施单位,征收范围,合计,住宅,非住宅,权证,人数,工企单位数,土地面积,概算资金)
select '" + iid + @"' IID,'0',
(select 申请单位 from zs_project where id='" + pid + @"')申请单位,
(select 项目名称 from zs_project where id='" + pid + @"')项目名称,
(select 项目地址 from zs_project where id='" + pid + @"')项目地址,
(select 征收部门 from zs_project where id='" + pid + @"')征收部门,
(select 征收实施单位 from zs_project where id='" + pid + @"')征收实施单位,
拆迁范围,
(select 被征收房屋面积合计 from zs_project where id='" + pid + @"')被征收房屋面积合计,
(select 被征收房屋面积住宅 from zs_project where id='" + pid + @"')被征收房屋面积住宅,
(select 被征收房屋面积非住宅 from zs_project where id='" + pid + @"')被征收房屋面积非住宅,
拆迁居民权证户数,
拆迁居民人数,
拆迁工企单位个数,
占地面积,
概算总额
from ZS_PRONO where id='" + ownerid + "'";
            _FlowFactoryProvider.RunSqlNon(sql);
        }
        #endregion

        #region 征收补偿决定
        public void ZSBuChang(string iid, string ownerid)
        {
            string sql = "";
            string pid = "";
            string pnid = "";
            //sql = @"select pid from zs_zzbcjfenhu where zspid='"+ownerid+"'";
            pnid = _FlowFactoryProvider.GetOneStrString("zs_zzbcjfenhu", "pid", "zspid", ownerid);
            pid = _FlowFactoryProvider.GetOneStrString("zs_prono", "pid", "id", pnid);
            sql = @"insert into UT_房屋征收补偿决定审批表
 (IID,input_index,征收决定号,项目名称,项目地点,征收部门,征收实施单位,产权人,公房承租人,被征收房屋坐落,被征收房屋建筑面积,被征收房屋用途,被征收房屋评估总价) 
select '" + iid + @"' IID,'0',
(select 征收许可证号 from zs_prono where id='" + pnid + @"')征收决定号,
(select 项目名称 from zs_project where id='" + pid + @"')项目名称,
(select 项目地址 from zs_project where id='" + pid + @"')项目地点,
(select 征收部门 from zs_project where id='" + pid + @"')征收部门,
(select 征收实施单位 from zs_project where id='" + pid + @"')征收实施单位,
产权人,
承租人,
房屋坐落,
房屋建筑面积,
房屋用途,
(select (to_number(t.zs室内装饰装修评估补偿金额合计)+to_number(t.fyy评估总价)+to_number(t.zz评估总价)+to_number(t.yy评估总价)) from zs_zzbcjfenhu t where t.zspid='" + ownerid + @"')被征收房屋评估总价
from zs_zsperson
where id='" + ownerid + "'";
            _FlowFactoryProvider.RunSqlNon(sql);
        }
        #endregion
    }
}


